<?php

class TblProjectFile extends CActiveRecord {

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
     
    public function tableName() {
        return 'project_file';
    }

    /**
     * @param $tblName
     * @param $contArr
     * @return bool
     */
    public function addRecord($tblName, $contArr) {
        $command = Yii::app()->db->createCommand();
        $result = $command->insert($tblName, $contArr);
        if($result) {
            return Yii::app()->db->getLastInsertID();
        } else {
            return false;
        }
    }

    /**
     * @param $condArr
     * @return bool
     */
    public function deleteRecord($condArr) {
        $result = $this::model()->find($condArr);
        if(empty($result)) {
            return true;
        }

        if($result->delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $cond
     * @return bool
     */
    public function deleteRecordAll($cond) {
        $result = $this::model()->deleteAll($cond);
        if($result >= 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * @param $condArr
     * @param $contArr
     * @return bool
     */
    public function updateRecord($condArr, $contArr) {
        $result = $this::model()->find($condArr);
        if (empty($result) || empty($contArr)) {
            return false;
        }
        foreach ($contArr as $key=>$value) {
            $result->$key = $value;
        }
        if(!$result->save()) {
            return false;
        }
        return true;
    }

    /**
     * @param array $condArr
     * @return null
     */
    public function selectRecord(array $condArr) {
        $result = $this::model()->find($condArr);
        if(!$result) {
            return NULL;
            exit;
        }
        return $result;
    }

    /**
     * @param array $condArr
     * @return null
     */
    public function selectRecordAll(array $condArr) {
        $result = $this::model()->findAll($condArr);
        if(!$result) {
            return NULL;
            exit;
        }
        return $result;
    }

}
